﻿using D.Util.UtilCache;
using D.UtilCore.Dtos;
using D.UtilCore.Models;
using Flurl.Http;
using Newtonsoft.Json;

namespace CozeDomain.Helpers
{
    /// <summary>
    /// Http请求
    /// </summary>
    public class HttpHelper<T>
    {
        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name=token></param>
        /// <returns></returns>
        public static async Task<T> GetAsync(string url, string token = null)
        {
            try
            {
                if (!string.IsNullOrEmpty(token))
                {
                    return await url.WithOAuthBearerToken(token).GetAsync().ReceiveJson<T>();
                }
               
                return await url.WithOAuthBearerToken(token).GetAsync().ReceiveJson<T>();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="t"></param>
        /// <param name=token></param>
        /// <returns></returns>
        public static async Task<T> PostAsync<T2>(string url, T2 t, string token = null)
        {
            try
            {
                if (!string.IsNullOrEmpty(token))
                {
                   return await url.WithOAuthBearerToken(token).PostJsonAsync(t).ReceiveJson<T>();
                }
                return await url.PostJsonAsync(t).ReceiveJson<T>();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Put请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="t"></param>
        /// <param name=token></param>
        /// <returns></returns>
        public static async Task<ApiResult<T>> PutAsync<T2>(string url, T2 t, string token = null)
        {
            try
            {
                if (!string.IsNullOrEmpty(token))
                {
                    return await url.WithOAuthBearerToken(token).PutJsonAsync(t).ReceiveJson<ApiResult<T>>();
                }

                return await url.PutJsonAsync(t).ReceiveJson<ApiResult<T>>();
            }
            catch (Exception ex)
            {
                return new ApiResult<T> { Code = StatusCodeEnum.Error, Msg = "请求失败" };
            }
        }

        /// <summary>
        /// Delete请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name=token></param>
        /// <returns></returns>
        public static async Task<ApiResult<T>> DeleteAsync(string url, string token = null)
        {
            try
            {
                if (!string.IsNullOrEmpty(token))
                {

                    return await url.WithOAuthBearerToken(token).DeleteAsync().ReceiveJson<ApiResult<T>>();
                }

                return await url.DeleteAsync().ReceiveJson<ApiResult<T>>();
            }
            catch (Exception ex)
            {
                return new ApiResult<T> { Code = StatusCodeEnum.Error, Msg = "请求失败" };
            }
        }
    }
}
